package com.controller;

import com.entity.Emp;
import com.entity.EmpFlatCheck;
import com.service.IEmpService;
import com.until.OSSUtil;
import com.until.Result;
import com.vo.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("emp")
public class EmpController extends BaseController{
    @Resource
    private IEmpService empService;

    @PostMapping("listByParam")
    public Result listByParam(@RequestBody Page<Emp> page){
        return success(empService.listByParam(page));
    }

    @PostMapping("listByStatus")
    public Result listByStatus(@RequestBody Page<Emp> page){
        return success(empService.listByStatus(page));
    }

    @GetMapping("login")
    public Result login(String username,String password){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            //System.out.println("11111111");
            return error("用户名和密码不能为空!!");
        }
        //收集用户信息
        AuthenticationToken authenticationToken = new UsernamePasswordToken(username, password);
        //获取Subject对象
        Subject currentUser = SecurityUtils.getSubject();

        try {
            //进行登录操作
            currentUser.login(authenticationToken);
            //获得安全session会话
            Session session = currentUser.getSession();
            //获取用户信息  在myRealm有获取认证数据的方法，方法的返回构造对象第一个参数就是Principal
            Emp emp=(Emp)currentUser.getPrincipal();
            //保存当前登录的用户
            session.setAttribute("empInfo",emp);
            return success("登录成功!!");
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        return error("用户名或密码错误!!");
    }
    @GetMapping("getLoginSession")
    public Result getLoginSession(HttpSession session){
        return success((Emp)session.getAttribute("empInfo"));
    }

    @GetMapping("getByEmpId")
    public Result getByEmpId(Integer empId){
        return success(empService.getByEmpId(empId));
    }

    @PostMapping("insert")
    public Result insert(@RequestBody Emp emp){
        return success(empService.insert(emp));
    }

    @PostMapping("insertFlat")
    public Result insertFlat(@RequestBody EmpFlatCheck empFlatCheck){
        return success(empService.insertFlat(empFlatCheck));
    }

    @PostMapping("update")
    public Result update(@RequestBody Emp emp){
        return success(empService.update(emp));
    }


    @PostMapping("updateStatus")
    public Result updateStatus(@RequestBody Emp emp){
        return success(empService.updateStatus(emp));
    }

    @PostMapping("updateFire")
    public Result updateFire(@RequestBody Emp emp){
        return success(empService.updateFire(emp));
    }

    @PostMapping("updateFreeze")
    public Result updateFreeze(@RequestBody Emp emp){
        return success(empService.updateFreeze(emp));
    }

    @PostMapping("updateUnfreeze")
    public Result updateUnfreeze(@RequestBody Emp emp){
        return success(empService.updateUnfreeze(emp));
    }


    @DeleteMapping("delete")
    public Result delete(Integer empId){
        return success(empService.delete(empId));
    }

    @RequestMapping(value = "listEmpFlatCheck",method = RequestMethod.POST)
    public Result listEmpFlatCheck(){
        return success(empService.listEmpFlatCheck());
    }

    @RequestMapping(value = "listEmpFlatHistory",method = RequestMethod.POST)
    public Result listEmpFlatHistory(){
        return success(empService.listEmpFlatHistory());
    }

    @PostMapping("updateFlat2")
    public Result updateFlat2(@RequestBody EmpFlatCheck empFlatCheck){
        return success(empService.updateFlat2(empFlatCheck));
    }

    @PostMapping("updateFlat3")
    public Result updateFlat3(@RequestBody EmpFlatCheck empFlatCheck){
        return success(empService.updateFlat3(empFlatCheck));
    }

    @DeleteMapping("deleteFlat")
    public Result deleteFlat(Integer empFlatId){
        return success(empService.deleteFlat(empFlatId));
    }

    @RequestMapping("uploadUserHeadPic")
    public Result uploadUserHeadPic(@RequestParam MultipartFile empImg) {
        return success(OSSUtil.uploadFile("emp/img",empImg));
    }


}
